package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.StarDao;
import com.example.librarysystem.entity.Star;
import com.example.librarysystem.entity.StarInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("starService")
public class StarServiceImpl implements StarService{
    @Autowired
    public StarDao starDao;

    @Override
    public Integer selectCount(){
        return starDao.selectCount();
    }
    @Override
    public Integer selectCountByBookName(String bookName){
        return starDao.selectCountByBookName(bookName);
    }

    @Override
    public List<StarInfo> selectStarInfo(int borrowerId, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return starDao.selectStarInfo(borrowerId, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<StarInfo> selectStarInfoSearch(int borrowerId, String bookName, String author, String publishingHouse, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return starDao.selectStarInfoSearch(borrowerId,bookName,author,publishingHouse,startCurrentPage,pageSize);
        }
    }
    @Override
    @Transactional(timeout = 30)
    public int deleteStarById(int id){
        starDao.deleteStarById(id);
        return 0;
    }
    @Override
    public int selectExist(String bookName, int borrowerId){
        return starDao.selectExist(bookName, borrowerId);
    }
    @Override
    @Transactional(timeout = 30)
    public int insertStar(Star star){
        starDao.insertStar(star);
        return 0;
    }
    @Override
    public int selectStarInfoCount(int borrowerId){
        return starDao.selectStarInfoCount(borrowerId);
    }
    @Override
    public int selectStarInfoSearchCount(int borrowerId, String bookName, String author, String publishingHouse){
        return starDao.selectStarInfoSearchCount(borrowerId,bookName,author,publishingHouse);
    }
}
